﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//自拼接图片的循环效果
public class Background : MonoBehaviour
{
    private const float screenWidth = (float)(320.0 / 9);   //屏幕宽度（u）
    private Transform transform_mainCamera;

    private Vector3 cameraPosition;
    private Vector3 offset;     //上一固定帧内相机的位移

    private float XPosition
    {
        get => transform.localPosition.x;
        set => transform.localPosition = new Vector3(value, 0, 10f);
    }

    private void Start()
    {
        transform_mainCamera = Camera.main.transform;
        cameraPosition = transform_mainCamera.position;
    }

    private void Update()
    {
        offset = transform_mainCamera.position - cameraPosition;
        cameraPosition = transform_mainCamera.position;
        XPosition -= offset.x;
        for (; XPosition > screenWidth;)
        {
            XPosition -= screenWidth;
        }
        for (; XPosition < -screenWidth;)
        {
            XPosition += screenWidth;
        }
    }
}
